//
//  ZMHorizontalGridView.m
//  Pods
//
//  Created by pg on 2017/9/13.
//
//

#import "ZMHorizontalGridView.h"
#import "ZMHorizontalGridViewLayout.h"
#import "ZMHorizontalGridViewCell.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>


@interface ZMHorizontalGridView ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>


@property (strong, nonatomic)   UICollectionView    *collectionView;


@property (strong, nonatomic)   UIPageControl           *pageControl;

@property (strong, nonatomic)   ZMHorizontalGridViewLayout  *layout;



@end


@implementation ZMHorizontalGridView{
    NSInteger _count;
    
}


// 刷新
- (void)reloadData {
    _count = 0;
    
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfItemsInHorizontalGridView:)]) {
        _count = [self.dataSource numberOfItemsInHorizontalGridView:self];
    }
    
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    if(self.mainColor ) {
        self.pageControl.currentPageIndicatorTintColor = self.mainColor;
    } else {
        self.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    }
    
    [self.collectionView reloadData];
    self.pageControl.numberOfPages = [self.layout numberOfPages];
    self.pageControl.currentPage = 0;
    
}




#pragma mark
#pragma mark - 初始化网格collectionview
- (UICollectionView *)collectionView {
    if(_collectionView == nil) {
        // 流式布局无法满足我们当下的需求 -> 自定义布局
        self.layout = [ZMHorizontalGridViewLayout new];
        
        //设置默认行数列数
        NSInteger row = 2;
        NSInteger column = 5;
        //如果实现代理方法  则从代理方法中获取列数
        if(self.delegate && [self.delegate respondsToSelector:@selector(numberOfColumsInHorizontalGridView:)]) {
            column = [self.delegate numberOfColumsInHorizontalGridView:self];
        }
        //如果实现行数则从代理方法中获取行数
        if(self.delegate && [self.delegate respondsToSelector:@selector(numberOfRowsInHorizontalGridView:)]) {
            row = [self.delegate numberOfRowsInHorizontalGridView:self];
        }
        
        self.layout.row = row;
        self.layout.column = column;
        
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        //注册cell
        [_collectionView registerClass:[ZMHorizontalGridViewCell class] forCellWithReuseIdentifier:@"CELL"];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.bottom.equalTo(self.pageControl.mas_top);
        }];
    }
    
    return _collectionView;
}
#pragma mark
#pragma mark - 分页指示器
- (UIPageControl *)pageControl {
    if(_pageControl == nil) {
        _pageControl = [UIPageControl new];
        [self addSubview:_pageControl];
        [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(30);
        }];
    }
    return _pageControl;
}

#pragma mark
#pragma mark - collectionView数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZMHorizontalGridViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
    // 标题
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(horizontalGridView:titleForItemAtIndex:)]) {
        cell.titleLabel.text = [self.dataSource horizontalGridView:self titleForItemAtIndex:indexPath.row];
    }
    
    // 图片
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(horizontalGridView:urlForItemAtIndex:)]) {
        NSURL *url = [self.dataSource horizontalGridView:self urlForItemAtIndex:indexPath.row];
        if(url != nil) {
            if(self.defaultIconImage == nil) {
                [cell.iconImageView sd_setImageWithURL:url];
            } else {
                [cell.iconImageView sd_setImageWithURL:url placeholderImage:self.defaultIconImage];
            }
            
            
        }
    }
    
    return cell;
}

#pragma mark
#pragma mark - 代理方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(80,80);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}


@end
